<?php
/***************************************************************************
 * 
 * Copyright (c) 2010 , Inc. All Rights Reserved
 * $Id$:HapNTester.php,2010/05/24 14:58:08 
 * 
 **************************************************************************/
 
 
 
/**
 * @file HapNTester.php
 * @author huqingping
 * @date 2010/05/24 14:58:08
 * @version 1.0 
 * @brief 
 *  
 **/

final class Response
{
	var $all = '';
	var $http = '';
	var $status = '';
	var $headers = array();
	var $body = '';
	var $redirect = '';
	var $cookies = array();
	function __construct($ret)
	{
		$this->all = $ret;
		$pos = strpos($ret,"\r\n\r\n");
		if (!$pos) {
			$this->redirect = $ret;
			return;
		}
		$header = substr($ret,0, $pos); 
		if (strpos($header, '100 Continue')) {
			$this->parse(substr($ret,$pos+4));
		} else {
			$this->parse($ret);
		}
	}

	function parse($ret)
	{
		$pos = strpos($ret,"\r\n\r\n");
		$header = substr($ret,0, $pos);
		$body = substr($ret,$pos+4);
		$headerLines = explode("\r\n",$header);
		$head = array_shift($headerLines);
		$pos = strpos($head,' ');
		$this->http = substr($head,0,$pos);
		$this->status = substr($head,$pos+1);
		foreach($headerLines as $line) {
			list($k,$v) = explode(":",$line);
			$k = trim($k);
			$v = trim($v);
			if ($k == 'Set-Cookie') {
				list($ck,$cv) = explode("=",$v);
				$this->cookies[trim($ck)] = trim($cv);
			} else {
				$this->headers[$k] = $v;
			}
		}
		$this->body = $body;
	}
	function json()
	{
		return json_decode($this->body,true);
	}
	function mcpack()
	{
		return mc_pack_pack2array($this->body);
	}
}

final class HapNTester
{
	static function run($testcase,$url,	$inputs	) 
	{
		$opt = self::buildOpts($inputs,$url);
		$ret = self::curl($url,$opt);
		if (!$ret) {
			$testcase->fail("curl exec failed $url");
		} else {
			return new Response($ret);
		}
	}

	static function buildOpts($inputs,&$url)
	{
		
		$opt = array(
			CURLOPT_RETURNTRANSFER=>1, 
			CURLOPT_HEADER=>1,
			CURLOPT_FOLLOWLOCATION=>1, 
			CURLOPT_ENCODING=>'',
			CURLOPT_USERAGENT=>'HapN',
			CURLOPT_AUTOREFERER=>1,                          
			CURLOPT_CONNECTTIMEOUT=>10, 
			CURLOPT_TIMEOUT=>30,
			CURLOPT_MAXREDIRS=>3,
			CURLOPT_VERBOSE=>CURL_DEBUG,
		);
		extract($inputs);
		//$gets = $posts = $headers = $cookies = array();
		if (!empty($get)) {
			$params = http_build_query($get);
			if (strpos($url,'?') !== false) {
				$url .= '&'.$params;
			} else {
				$url .= '?'.$params;
			}
		}
		if (!empty($put)) {
			$fp = fopen($put,'rb');
			$stat = fstat($fp);
			$size = $stat['size'];
			$opt[CURLOPT_INFILE] = $fp;
			$opt[CURLOPT_INFILESIZE] = $size;
		} 
		if (!empty($post)) {
			$opt[CURLOPT_POSTFIELDS] = http_build_query($post);
		}
		if (!empty($put)) {  
			$opt[CURLOPT_PUT] = true;         
		} elseif (!empty($post)) {     
			$opt[CURLOPT_POST] = true;   
		} else {
			$opt[CURLOPT_HTTPGET] = true;
		}
		if (!empty($header)) {
			$opt[CURLOPT_HTTPHEADER] = $header;
		}
		if (!empty($cookie)) {
			$arrcookie = array();
			foreach($cookie as $k=>$v) {
				$arrcookie[] = "$k=$v";
			}
			$opt[CURLOPT_COOKIE] = implode('; ',$arrcookie);	
		}
		return $opt;
	}
	static function curl(&$url,$opts)
	{
		$curl = curl_init($url);
		curl_setopt_array($curl, $opts);
		$ret = curl_exec($curl);
		if ($ret === false) {
			$info = (curl_getinfo($curl));
			if ($info['http_code'] == 301 ||
				$info['http_code'] == 302) {
					$ret = $info['url'];
			}
		}
		curl_close($curl);
		return $ret;
	}

}




/* vim: set ts=4 sw=4 sts=4 tw=100 noet: */
?>
